package com.jule.teenpatti.game.service.holder;

import JoloProtobuf.GameSvr.JoloGame;
import com.jule.teenpatti.game.dao.DBUtil;
import com.jule.teenpatti.game.dao.bean.GiftConfigModel;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class GiftConfigHolder {
    private static Map<String, GiftConfigModel> GIFT_CONFIG =new HashMap<>(); //所有礼物的配置
    private static Map<String, JoloGame.JoloGame_ItemInfo> GIFT_ITEM_MAP =new HashMap<>(); //所有礼物的配置

    private static class SingletonHolder {
        protected static final GiftConfigHolder instance = new GiftConfigHolder();
    }

    public static final GiftConfigHolder getInstance() {
        return GiftConfigHolder.SingletonHolder.instance;
    }

    private GiftConfigHolder(){
        init();
    }

    /**
     *
     * @param giftId
     * @return
     */
    public GiftConfigModel getGiftConfig(String giftId) {
        return GIFT_CONFIG.get(giftId);
    }

    /**
     * 获取所有配置
     *
     * @return
     */
    public Collection<JoloGame.JoloGame_ItemInfo> getAllGiftConfig() {
        return GIFT_ITEM_MAP.values();
    }

    /**
     * 初始化加载内存
     */
    public boolean init(){
        try{
            List<GiftConfigModel> configList = DBUtil.getGiftConfigFromDb();
            for(int i=0; i < configList.size(); i++){
                GiftConfigModel model = configList.get(i);
                GIFT_CONFIG.put(model.getId(),model);
                GIFT_ITEM_MAP.put(model.getId(), JoloGame.JoloGame_ItemInfo.newBuilder().setItemPrice(model.getPrice())
                        .setItemExtraPrice(model.getExtraPrice())
                        .setItemDesc(model.getDesc()).setItemIcon(model.getIcon()).setItemId(model.getId()).setItemName(model.getName())
                        .setItemType(model.getItemType()).build());
            }
            log.info("加载 " + GIFT_CONFIG.size() + " 礼物配置");
            return true;
        }catch(Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
